package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.tentative.competition.common.constant.Constants;
import com.tentative.competition.common.constant.UserConstants;
import com.tentative.competition.common.core.domain.entity.SysRole;
import com.tentative.competition.common.core.domain.entity.SysUser;
import com.tentative.competition.common.core.domain.model.LoginUser;
import com.tentative.competition.common.enums.LoginTypeEnum;
import com.tentative.competition.common.exception.user.BlackListException;
import com.tentative.competition.common.exception.user.LoginRoleException;
import com.tentative.competition.common.exception.user.UserNotExistsException;
import com.tentative.competition.common.exception.user.UserPasswordNotMatchException;
import com.tentative.competition.common.model.schedule.param.LoginParam;
import com.tentative.competition.common.utils.MessageUtils;
import com.tentative.competition.common.utils.StringUtils;
import com.tentative.competition.common.utils.ip.IpUtils;
import com.tentative.competition.common.utils.uuid.UUID;
import com.tentative.competition.schedule.service.ScheduleLoginService;
import com.tentative.competition.system.mapper.SysRoleMapper;
import com.tentative.competition.system.service.ISysConfigService;
import com.tentative.competition.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @auther: Smile Huang
 * @date: 2023/6/22 21:09
 * @desc:
 * @version: 2.0
 */
@Service
public class ScheduleLoginServiceImpl implements ScheduleLoginService {

//    @Lazy
//    @Autowired
//    private ISysConfigService configService;
//
//    /**
//     * 登录前置校验
//     *
//     * @param username 用户名
//     */
//    public void loginPreCheck(String username) {
//        // 用户名或密码为空 错误
//        if (StringUtils.isEmpty(username)){
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
//            throw new UserNotExistsException();
//        }
//        // 用户名不在指定范围内 错误
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
//            throw new UserPasswordNotMatchException();
//        }
//        // IP黑名单校验
//        String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
//        if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
//            throw new BlackListException();
//        }
//    }
//
//    private Boolean verifyEmail(String email){
//        String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
//        return Pattern.matches(emailMatcher,email);
//    }
}
